package com.campuscrowdsourcing.server.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campuscrowdsourcing.server.annotation.Log;
import com.campuscrowdsourcing.server.common.enums.Action;
import com.campuscrowdsourcing.server.domain.entity.TeamRole;
import com.campuscrowdsourcing.server.domain.model.AjaxResult;
import com.campuscrowdsourcing.server.service.TeamRoleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 团队权限	如：管理员、老师、项目经理 前端控制器
 * </p>
 *
 * @author Xinyuan Zhao
 * @since 2023-03-15
 */
@RestController
@RequestMapping("/api/teamRole")
public class TeamRoleController {
    @Resource
    private TeamRoleService teamRoleService;

    @PostMapping
    @Log(title = "新增团队身份", action = Action.INSERT)
    public AjaxResult saveTeamRole(@RequestBody TeamRole teamrole) {
        teamRoleService.save(teamrole);
        return AjaxResult.success();
    }

    @PutMapping
    @Log(title = "编辑团队身份", action = Action.UPDATE)
    public AjaxResult updateTeamRole(@RequestBody TeamRole teamrole) {
        teamRoleService.updateById(teamrole);
        return AjaxResult.success();
    }

    @DeleteMapping("/{id}")
    @Log(title = "删除团队身份", action = Action.DELETE)
    public AjaxResult deleteTeamRole(@PathVariable Integer id) {
        teamRoleService.removeById(id);
        return AjaxResult.success();
    }

    @PostMapping("/del/batch")
    @Log(title = "批量删除团队身份", action = Action.DELETE)
    public AjaxResult deleteBatchTeamRole(@RequestBody List<Integer> ids) {
        teamRoleService.removeByIds(ids);
        return AjaxResult.success();
    }

    @GetMapping
    @Log(title = "查看团队身份", action = Action.SELECT)
    public AjaxResult listTeamRole() {
        return AjaxResult.success(teamRoleService.list());
    }

    @GetMapping("/{id}")
    @Log(title = "id查询团队身份", action = Action.SELECT)
    public AjaxResult getTeamRoleById(@PathVariable Integer id) {
        TeamRole teamRole = teamRoleService.getById(id);
        if(teamRole == null)
        {
            return AjaxResult.fail("该身份不存在或已删除");
        }
        return AjaxResult.success(teamRole);
    }

    @GetMapping("/page")
    @Log(title = "团队身份分页查询", action = Action.SELECT)
    public AjaxResult listTeamRolePage(@RequestParam(defaultValue = "") String name,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<TeamRole> queryWrapper = new QueryWrapper<TeamRole>().orderByDesc("id");
        queryWrapper.like(!"".equals(name), "name", name);
        return AjaxResult.success(teamRoleService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }
}

